<script setup lang="ts">
import { computed, nextTick, reactive, watch } from 'vue';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import { unitCreateApi, unitEditApi } from '@/service/api/businessApi';

defineOptions({
  name: 'UnitOperateDrawer'
});

interface Props {
  operateType: AntDesign.TableOperateType;
  rowData?: any | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const { formRef, validate, resetFields } = useAntdForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<AntDesign.TableOperateType, string> = {
    add: '新增单位',
    edit: '编辑单位'
  };
  return titles[props.operateType];
});

const model: any = reactive(createDefaultModel());

function createDefaultModel(): any {
  return {
    sex: ''
  };
}

const rules: any = {
  name: defaultRequiredRule,
  principal: defaultRequiredRule,
  address: defaultRequiredRule
};

/** the enabled role options */
async function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.operateType === 'edit' && props.rowData) {
    await nextTick();
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

const doAddSubmit = async () => {
  const res = await unitCreateApi({
    ...model
  });
  if (res.error === null) {
    window.$message?.success('新增成功');
    closeDrawer();
    emit('submitted');
  }
};
const doEditSubmit = async () => {
  const res = await unitEditApi({
    ...model
  });
  if (res.error === null) {
    window.$message?.success('更新成功');
    closeDrawer();
    emit('submitted');
  }
};

async function handleSubmit() {
  await validate();
  if (props.operateType === 'add') {
    await doAddSubmit();
  }
  if (props.operateType === 'edit') {
    await doEditSubmit();
  }
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
    resetFields();
  }
});
</script>

<template>
  <AModal v-model:open="visible" :title="title" :width="560" :mask-closable="false">
    <AForm ref="formRef" layout="vertical" :model="model" :rules="rules">
      <AFormItem label="单位名称" name="name">
        <AInput v-model:value="model.name" placeholder="请输入单位名称" />
      </AFormItem>
      <AFormItem label="单位负责人" name="principal">
        <AInput v-model:value="model.principal" placeholder="请输入单位负责人" />
      </AFormItem>
      <AFormItem label="联系人手机号" name="phone">
        <AInput v-model:value="model.phone" placeholder="请输入联系人手机号" />
      </AFormItem>
      <AFormItem label="单位地址" name="address">
        <AInput v-model:value="model.address" placeholder="请输入单位地址" />
      </AFormItem>
      <AFormItem label="简介标题" name="brief">
        <AInput v-model:value="model.brief" placeholder="请输入简介标题" />
      </AFormItem>
      <AFormItem label="简介描述" name="detail">
        <AInput v-model:value="model.detail" placeholder="请输入简介描述" />
      </AFormItem>
      <AFormItem label="萤石AppKey" name="appKey">
        <AInput v-model:value="model.appKey" placeholder="请输入appKey" />
      </AFormItem>
      <AFormItem label="萤石serect" name="appSecret">
        <AInput v-model:value="model.appSecret" placeholder="请输入appSecret" />
      </AFormItem>
    </AForm>
    <template #footer>
      <ASpace :size="16">
        <AButton @click="closeDrawer">取消</AButton>
        <AButton type="primary" @click="handleSubmit">确认</AButton>
      </ASpace>
    </template>
  </AModal>
</template>

<style scoped></style>
